package com.amat0urheu.controller.teacherController;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.alibaba.fastjson.JSONObject;
import com.amat0urheu.annotation.OperateLog;
import com.amat0urheu.config.security.MyUserDetails;
import com.amat0urheu.dao.ExperimentsMapper;
import com.amat0urheu.pojo.*;
import com.amat0urheu.pojo.CloudPojo.OpenStack;
import com.amat0urheu.service.ExperimentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
@RequestMapping("/api")
public class ExperimentController {
    final ExperimentService experimentService;

    @Autowired
    public ExperimentController(ExperimentService experimentService) {
        this.experimentService = experimentService;
    }

    @OperateLog(description="1",action="查询实验")
    @GetMapping("/experiment/{pageSize}/{pageNum}")
    public ReturnRes getExperimentpage(@PathVariable int pageSize,
                                   @PathVariable int pageNum) {
        return  experimentService.getExperimentpage(pageSize,pageNum);
    }

    @GetMapping("/user/experiment/{id}")
    public ReturnRes getExperimentByIdForUser(@PathVariable String id) {

        return experimentService.getExperimentByIdForUser(id);
    }

   /* @GetMapping("/user/experiment/apply/{eid}")
    public ReturnRes applyExperimentForUser(@PathVariable Integer eid,Authentication authentication) {
        System.out.println(authentication.getName());
        return experimentService.getExperimentByIdForUser(eid);
    }*/

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/admin/experiment")
    public ReturnRes operate(@RequestParam String action,
                             @RequestParam(name = "data") String experimentJson) {


        Experiment experiment= JSONObject.parseObject(experimentJson, Experiment.class);
        if (action.equals("save")) {
            experiment.setCreateTime(new Date());

            return  experimentService.saveExperiment(experiment);
        }else if(action.equals("update")){
            return experimentService.updateExperiment(experiment);
        } else {
            return new ReturnRes(false, "action value error");
        }
    }
    //查询实验与场景的映射信息
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @OperateLog(description = "1" ,action="查询实验与场景的映射信息")
    @GetMapping("/experiment/scene/{pageSize}/{pageNum}")
    public ReturnRes getExperimentAndScene(@PathVariable int pageSize,
                                       @PathVariable int pageNum) {
        return  experimentService.getExperimentAndScene(pageSize,pageNum);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/experiment/scene/remove")
    public ReturnRes removeExperimentAndScene(@RequestParam(name = "data") String ExperimentSceneJson) {

        ExperimentScene experimentScene = JSONObject.parseObject(ExperimentSceneJson, ExperimentScene.class);

        return experimentService.removegetExperimentAndScene(experimentScene.getId());
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/experiment/getAllExperimentVm/{pageSize}/{pageNum}")
    public ReturnRes getAllExperimentVm(@PathVariable int pageSize,
                                           @PathVariable int pageNum) {
        return  experimentService.getAllExperimentVm(pageSize,pageNum);
    }

/*    @GetMapping("/experiment/getUserExperimentVm")
    public ReturnRes getExperimentVmsforUser(Authentication authentication) {
        return  experimentService.getExperimentVmsforUser(authentication.getName());
    }*/
    @PostMapping("/experiment/getUserExperimentVm")
    ReturnRes getExperimentVmsforUser(@RequestParam(name = "id") Integer id,Authentication authentication) {
        return  experimentService.getExperimentVmsforUser(authentication.getName(),id);
    }

    @GetMapping("/experiment/applyVmForUser/{experimentName}")
    public ReturnRes applyVmForUser(@PathVariable String  experimentName,Authentication authentication) {


        return  experimentService.applyVmServerForUser(experimentName,authentication.getName());
    }

    @GetMapping("/admin/experiment")
    public ReturnRes getexperiments() {
        return  experimentService.getexperiments();
    }

    @PostMapping("/admin/experiment/ExperimentAndSceneAddSubmit")
    public ReturnRes getExperimentVmsPage(
                             @RequestParam(name = "experimentId") Integer experimentId,  @RequestParam(name = "sceneId") Integer sceneId) {
            return experimentService.saveExperimentAndSceneAdd(experimentId,sceneId);
    }

    @PostMapping("/admin/experiment/ExperimentAndSceneupdateSubmit")
    public ReturnRes ExperimentAndSceneupdateSubmit(@RequestParam(name = "data") String experimentSceneJson) {
        ExperimentScene experimentScene = JSONObject.parseObject(experimentSceneJson, ExperimentScene.class);
        return experimentService.ExperimentAndSceneupdate( experimentScene);
    }

    @PostMapping("/admin/experiment/removeExperimentAndScene")
    public ReturnRes removeExperimentAndScene(
            @RequestParam(name = "id") Integer id) {

        return experimentService.removeExperimentAndScene(id);
    }

/*    @OperateLog(description="1",action="查询实验")*/
    @GetMapping("/admin/experiment/getExperimentScores/{pageSize}/{pageNum}")
    public ReturnRes getExperimentScores(@PathVariable int pageSize,
                                       @PathVariable int pageNum) {
        return  experimentService.getExperimentScores(pageSize,pageNum);
    }
    @GetMapping("/admin/experiment/getAllExperiments")
    public ReturnRes getAllExperiments() {
        return  experimentService.getAllExperiments();
    }
    @GetMapping("/admin/experiment/getAllUserScores/{pageSize}/{pageNum}")
    public ReturnRes getAllUserScores(@PathVariable int pageSize,
                                           @PathVariable int pageNum) {
        return  experimentService.getAllUserScores(pageSize,pageNum);
    }

    @PostMapping("/admin/experiment/updateScoreOfUser")
    public ReturnRes updateScoreOfUser(@RequestParam(name = "data") String userScoreJson) {
        ExperimentScores experimentScores = JSONObject.parseObject(userScoreJson, ExperimentScores.class);
        return    experimentService.updateUserScore(experimentScores);
    }
    @GetMapping("/admin/experiment/search/{pageSize}/{pageNum}")
    public ReturnRes searchExperimentForAdmin(@PathVariable int pageSize,
                                   @PathVariable int pageNum,
                                   @RequestParam String key,
                                   @RequestParam String value) {
        return experimentService.searchExperimentForAdmin(key, value, pageNum, pageSize);
    }

    @GetMapping("/admin/experiment/searchES/{pageSize}/{pageNum}")
    public ReturnRes searchExperimentAndSceneForAdmin(@PathVariable int pageSize,
                                              @PathVariable int pageNum,
                                              @RequestParam String key,
                                              @RequestParam String value) {
        return experimentService.searchExperimentAndSceneForAdmin(key, value, pageNum, pageSize);
    }
    @GetMapping("/admin/experiment/searchScore/{pageSize}/{pageNum}")
    public ReturnRes searchExperimentScorePage(@PathVariable int pageSize,
                                                      @PathVariable int pageNum,
                                                      @RequestParam String key,
                                                      @RequestParam String value) {
        return experimentService.searchExperimentScorePage(key, value, pageNum, pageSize);
    }

    @GetMapping("/admin/experiment/experimentVm/search/{pageSize}/{pageNum}")
    public ReturnRes searchExperimentVmPage(@PathVariable int pageSize,
                                        @PathVariable int pageNum,
                                        @RequestParam String key,
                                        @RequestParam String value) {
        return experimentService.searchExperimentVmPage(key, value, pageNum, pageSize);
    }

    @PostMapping("/admin/experiment/uploadImage")
    public ReturnRes uploadImageForWP(@RequestParam(name = "file") MultipartFile file,
                                      @RequestParam Integer eid) {

        return experimentService.uploadImageForExperiment(file,eid);//ctfFileService.uploadImageForWP(file, wid);
    }

    @GetMapping("/admin/getExperimentvmCount")
    public ReturnRes getRacevmCount() {
        return experimentService.getExperimentvmCount();
    }


    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/user/downloadScoreFile")
    public void downloadScoreFile(HttpServletResponse response) {

        try {
            experimentService.downloadScoreFile(response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/user/experiment/releaseVm")
    public ReturnRes releaseExperimentVm(@RequestParam(name = "eid")Integer eid,Authentication authentication) {
        return   experimentService.releaseExperimentVm(eid);/*raceService.releaseRaceVm(authentication.getName(),rid)*/
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/admin/removeExperiment")
    public ReturnRes removeExperiment(@RequestParam(name = "data") String experimentJson) {

        Experiment experiment = JSONObject.parseObject(experimentJson, Experiment.class);

        return experimentService.removegetExperiment(experiment.getEid());
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/admin/experiment/releaseAllExpeirmentVms")
    public ReturnRes releaseAllExpeirmentVms() {
        return   experimentService.releaseAllExpeirmentVms();/*raceService.releaseRaceVm(authentication.getName(),rid)*/
    }
}

